package es.deusto.tralala.rmi.server;

import java.rmi.Naming;
import java.rmi.RMISecurityManager;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import es.deusto.tralala.data.Cancion;
import es.deusto.tralala.data.Reproduccion;
import es.deusto.tralala.data.Usuario;
import es.deusto.tralala.data.assembler.CancionAssembler;
import es.deusto.tralala.data.dto.CancionDTO;
import es.deusto.tralala.data.dto.ReproduccionDTO;
import es.deusto.tralala.db.IDAO;
import es.deusto.tralala.db.JDO;
import es.deusto.tralala.gateway.Gateway;

public class TralalaFacade extends UnicastRemoteObject implements ITralalaFacade {

	private static final long serialVersionUID = 1L;
	private static TralalaFacade instance = null;
	
	private IDAO jdo = new JDO();
	private Gateway gateway = new Gateway("AuthServer");
	private Map<String, Usuario> clients = new HashMap<>();

	private TralalaFacade() throws RemoteException {
	}
	
	public static TralalaFacade getInstance() {
		if (instance != null) {
			return instance;
		}
		else {
			try {
				instance = new TralalaFacade();
			} catch (RemoteException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return instance;
	}

	public String sayHello() {
		return "Hello World!";
	}
	
	@Override
	public boolean login(String uid, String user, String pass) throws RemoteException {
		if (gateway.login(user, pass)) {
			Usuario usuario = jdo.getUsuario(user);
			
			if (usuario != null) {
				clients.put(uid, usuario);
				return true;
			}
			
			return false;
		}
		else {
			return false;
		}
	}
	
	@Override
	public List<CancionDTO> searchCancion(String text) throws RemoteException {
		return CancionAssembler.assemble(jdo.searchCancion(text));
	}
	
	@Override
	public List<CancionDTO> getCanciones() throws RemoteException {
		return CancionAssembler.assemble(jdo.getCanciones());
	}
	
	@Override
	public CancionDTO getCancion(String title) throws RemoteException {
		Cancion cancion = jdo.getCancion(title);
		return new CancionDTO(cancion.getTitulo(), cancion.getArtista(), cancion.getGenero());
	}
	
	@Override
	public void addReproduccion(String uid, ReproduccionDTO reproduccion) throws RemoteException {
		if (clients.containsKey(uid)) {
			jdo.addReproduccion(new Reproduccion(clients.get(uid), jdo.getCancion(reproduccion.getCancion().getTitulo())));
		}
	}

	public static void main(String[] args) {
		if (args.length != 3) {
			System.out.println("usage: java [policy] [codebase] server.Server [host] [port] [server]");
			System.exit(0);
		}

		if (System.getSecurityManager() == null) {
			System.setSecurityManager(new RMISecurityManager());
		}

		String name = "//" + args[0] + ":" + args[1] + "/" + args[2];

		try {
			ITralalaFacade objServidor = TralalaFacade.getInstance();
			Naming.rebind(name, objServidor);
			System.out.println("* Server '" + name + "' active and waiting...");
		} catch (Exception e) {
			System.err.println("- Exception running the server: " + e.getMessage());
			e.printStackTrace();
		}
	}
}
